package com.ymatou.iapi.smsproxy.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.smsproxy.parameter.SendMessageBean;
import com.ymatou.iapi.smsproxy.parameter.SmsCallBackBean;
import com.ymatou.iapi.smsproxy.service.SendMessageCall;
import com.ymatou.iapi.smsproxy.service.SmsCallBackCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.SendSmsCallService;
import com.ymttest.database.mongo.SmsRecordBean;
import com.ymttest.database.mongo.SmsRecordWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
 * 短信回调 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_SmsCallBack {
	private static SmsCallBackBean smscallbackBean;
	private static SmsCallBackCall smscallbackCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("短信回调");
	}

	@Before
	public void caseUp() {
		smscallbackBean = new SmsCallBackBean();
		smscallbackCall = new SmsCallBackCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(smscallbackCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verifyDB(SmsCallBackBean scallbackbean) throws Exception
	{
		
		Logger.comment("根据appid、content、phone到数据库表SmsRecord中查询相应记录来验证是否存入SmsRecord表");
		
		List<String> phones=new ArrayList<String>();
		phones.add(scallbackbean.getPhone());
		
		SmsRecordBean smsRecord = new SmsRecordWapper().getCollectionByAppIdContentPhones(scallbackbean.getAppId(), scallbackbean.getContent(), phones);

		Logger.verifyNotNull(smsRecord, "验证数据存入SmsRecord成功");
		Logger.debug(com.ymt.core.tool.FormatUtil.beanToGSONString(smsRecord));
		//{"_id":"201712-654f723a33314e5995bd7826b35df127","AppId":"app-java-teleSign","ApiName":"SendMessage","ChannelId":"java-teleSign","ChannelName":"teleSign","Phones":[SendSmsCallService.TESTPHONE],"Content":"autotest_发送短信_6106515124","CreatedTime":{"$date":"2017-12-05T02:44:30.424Z"},"SendedTime":{"$date":"2017-12-05T02:44:32.360Z"},"ClientIp":"172.16.101.12:61584","ServerIp":"172.16.101.112","MessageAction":6,"Comment":"java app","ResponseMessage":"{\"resource_uri\": \"/v1/verify/2580923D69A8070490409157579C048A\", \"reference_id\": \"2580923D69A8070490409157579C048A\", \"sub_resource\": \"sms\", \"errors\": [], \"verify\": {\"code_state\": \"UNKNOWN\", \"code_entered\": \"\"}, \"status\": {\"updated_on\": \"2017-12-05T02:44:22.871057Z\", \"code\": 290, \"description\": \"Message in progress\"}}","Number":1}
		Logger.verifyEquals(scallbackbean.getClientIp(),smsRecord.getClientIp(), "验证ClientIp");
		Logger.verifyEquals(scallbackbean.getServerIp(),smsRecord.getServerIp(), "验证ServerIp");
		
		Logger.verifyEquals(scallbackbean.getAppId(),smsRecord.getAppId(),"验证AppId");
		Logger.verifyEquals(scallbackbean.getApiName(),smsRecord.getApiName(),"验证ApiName");
		

		Logger.verifyEquals(scallbackbean.getContent(),smsRecord.getContent(),"验证Content");
		Logger.verifyIsContains(scallbackbean.getPhone(),smsRecord.getPhones(),"验证Phone");
		Logger.verifyEquals(scallbackbean.getId(),smsRecord.get_id(),"验证id");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_SmsCallBack_001() {
		Logger.start(true,"短信回调app-java-teleSign成功");
		try {
			String appid="app-java-teleSign";
			String phone=SendSmsCallService.TESTPHONE;
			String content= "test"+TestDataManager.getRandomNum(5);
			String id=YMTDateUtil.getSysTime("YYYYMM")+"-"+TestDataManager.getRandomUUID().toString();
			
			smscallbackBean=SendSmsCallService.initSmsCallBackBean(id, appid, phone, content);
			smscallbackCall.setData(smscallbackBean);
			smscallbackCall.callService();			
			Logger.verifyEquals("ok", smscallbackCall.getReturnData(), "验证返回");
			Thread.sleep(2000);
			verifyDB(smscallbackBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}